package characterstreamzifu;

import java.io.*;

public class File {
    //从文件中读取字符数据到内存的字符输入流
    public static void a() throws IOException {
        FileReader fileReader = new FileReader("src/bb.txt");
        int num;
        while ((num = fileReader.read()) != -1) {
            System.out.println("num = " + (char) num);
        }
    }

    //将字符数据写入到文件的输出流
    public static void b() throws IOException {
        FileWriter fileWriter = new FileWriter("src/bb.txt");
        String name = "王航波";
        fileWriter.write(name);
        fileWriter.flush();
//        fileWriter.close();
    }

    //    带有缓冲区的字符输入流
    public static void c() throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/bb.txt"));
        String txt = "jiana,再见";
        bufferedWriter.write(txt);
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    //    带有缓冲区的字符输出流
    public static void d() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src/bb.txt"));
        int num;
        while ((num = bufferedReader.read()) != -1) {
            System.out.println("num = " + (char) num);
        }
    }

    //字符流--》字节流
    public static void e() throws IOException {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("src/bb.txt"),"utf-8");
        String data="胡志维是个女生";
        outputStreamWriter.write(data);
        outputStreamWriter.flush();
        outputStreamWriter.close();
    }
    //从字符串中读取字符流
    public static void f() throws IOException {
        String data="jianawhb";
        StringReader stringReader = new StringReader(data);
        int num;
        while((num=stringReader.read())!=-1){
            System.out.println("num = " + (char)num);
        }
    }
    //写入字符到字符串，将字符数据写入到字符串中的字符输出流，将程序中生成的字符数据写入字符串中方便处理
    public static void g(){
        StringWriter stringWriter = new StringWriter();
        String data="hello,world";//程序中生成的子数据
        stringWriter.write(data);
        System.out.println("stringWriter.toString() = " + stringWriter.toString());
    }
    public static void h(){

    }
    public static void main(String[] args) throws IOException {
//        a();
//        b();
//        c();
//        d();
//        e();
//        f();
//        g();
    }
}
